/**********************************************************
 * Copyright © 2008，北京福富软件技术股份有限公司
 * All Rights Reserved.
 *
 * 文件名称： DownloadStreamProvider.java
 * 摘    要：
 *
 * 初始版本：1.0.0
 * 原 作 者：RUN
 * 完成日期：2009-9-10
 *
 * 当前版本：$LastChangedRevision$
 * 作    者：$LastChangedBy$
 * 完成日期：$LastChnagedDate$
 ************************************************************/
package aip.ui.user.web.spring;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

import org.patchca.background.SingleColorBackgroundFactory;
import org.patchca.color.RandomColorFactory;
import org.patchca.filter.predefined.CurvesRippleFilterFactory;
import org.patchca.filter.predefined.DiffuseRippleFilterFactory;
import org.patchca.filter.predefined.DoubleRippleFilterFactory;
import org.patchca.filter.predefined.MarbleRippleFilterFactory;
import org.patchca.filter.predefined.WobbleRippleFilterFactory;
import org.patchca.service.Captcha;
import org.patchca.service.ConfigurableCaptchaService;

import com.googlecode.horncomb.web.spring.mvc.ServletResponeInputStreamProvider;
import com.googlecode.horncomb.web.spring.mvc.ServletResponeInputStreamSource;

/**
 * 获取附件
 * <p>
 * 负责人/principal: Daniel<br/>
 * 修改记录/revision:<br/>
 * 日期:<br/>
 * 修改人:<br/>
 * 修改说明:
 * @author RUN
 * @version $Id: codetemplates.xml 2007 2009-05-08 08:36:14Z shiw $
 */
public class VerifyPicStreamProvider implements
        ServletResponeInputStreamProvider
{
    /**
     * {@inheritDoc}
     */
    public ServletResponeInputStreamSource getInputStreamSource(
            HttpServletRequest request) throws Exception
    {
        ConfigurableCaptchaService cs = new ConfigurableCaptchaService();
        cs.setHeight(50);
        cs.setWidth(155);
        cs.setColorFactory(new RandomColorFactory());
        cs.setBackgroundFactory(new SingleColorBackgroundFactory(new Color(253,
                253, 253)));
        switch ((int) Math.round(Math.random() * 10) % 5)
        {
            case 0:
                cs.setFilterFactory(new CurvesRippleFilterFactory(cs
                        .getColorFactory()));
                break;
            case 1:
                cs.setFilterFactory(new MarbleRippleFilterFactory());
                break;
            case 2:
                cs.setFilterFactory(new DoubleRippleFilterFactory());
                break;
            case 3:
                cs.setFilterFactory(new WobbleRippleFilterFactory());
                break;
            case 4:
                cs.setFilterFactory(new DiffuseRippleFilterFactory());
        }
        Captcha verifyCode = cs.getCaptcha();
        request.getSession().setAttribute("verifyCode",
                verifyCode.getChallenge());
        BufferedImage img = verifyCode.getImage();

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(img, "png", os);

        byte[] buf = os.toByteArray();
        InputStream is = new ByteArrayInputStream(buf);

        return new ServletResponeInputStreamSource("verifycode", is);
    }
}
